//
//  QLMaskUtil.m
//  QLSchool_IOS
//
//  Created by anan on 14-2-10.
//  Copyright (c) 2014年 baobaodev_anan. All rights reserved.
//

#import "QLMaskUtil.h"

@implementation QLMaskUtil

+(void)showMaskLoadingWithView:(UIView*)view
{
    [self hideLoadingMaskWithView:view];
    NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"QLMaskLoadingView" owner:self options:nil];
    QLMaskLoadingView *loadingView = [nibViews objectAtIndex:0];
    loadingView.frame = view.bounds;
    Class clazz = [UITableView class];
    Class clazz2 = [UICollectionView class];
    Class parenClazz = [view class];
    if (clazz == parenClazz||clazz2==parenClazz) {
        ((UITableView*)view).scrollEnabled = NO;
    }
    [view addSubview:loadingView];
    [loadingView startAnimating];
}

+(BOOL)haveLoadingViewWithView:(UIView*)view
{
    if (!view||!view.subviews) {
        return NO;
    }
    for (id tmpView in view.subviews) {
        if ([tmpView class] == [QLMaskLoadingView class]) {
            return YES;
        }
    }
    return NO;
}

+(void)hideLoadingMaskWithView:(UIView*)view
{
    if (!view||!view.subviews) {
        return;
    }
    QLMaskLoadingView *modelView = nil;
    for (id tmpView in view.subviews) {
        if ([tmpView class] == [QLMaskLoadingView class]) {
            modelView = (QLMaskLoadingView*)tmpView;
            [modelView removeFromSuperview];
            Class clazz = [UITableView class];
            Class clazz2 = [UICollectionView class];
            Class parenClazz = [view class];
            if (clazz == parenClazz||clazz2==parenClazz) {
                ((UITableView*)view).scrollEnabled = YES;
            }
        }
    }
}

+(void)showMaskNodataWithView:(UIView*)view andOffsetY:(int)y andImage:(UIImage*)image andTitle:(NSString*)title andDelegate:(id<QLMaskNodataViewOnClickDelegate>) delegate
{
    [self hideNodataMaskWithView:view];
    NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"QLMaskNodataView" owner:self options:nil];
    QLMaskNodataView *nodataView = [nibViews objectAtIndex:0];
    nodataView.delegate = delegate;
    nodataView.frame = view.bounds;
    Class clazz = [UITableView class];
    Class parenClazz = [view class];
    if (clazz == parenClazz) {
        ((UITableView*)view).scrollEnabled = NO;
    }
    [view addSubview:nodataView];
    
    CGRect rect = CGRectMake(
                             nodataView.contentView.frame.origin.x,
                             (view.frame.size.height-nodataView.contentView.frame.size.height)/2+y,
                             nodataView.contentView.frame.size.width,
                             nodataView.contentView.frame.size.height);
    nodataView.contentView.frame = rect;
    if (image) {
        [nodataView.imageView setImage:image];
    }
    if (title) {
        CGRect r = nodataView.lableView.frame;
        r.size.width = 200;
        nodataView.lableView.numberOfLines = 0;
        nodataView.lableView.frame = r;
        nodataView.lableView.text = title;
        [nodataView.lableView sizeToFit];
    }
}

+(void)showMaskNodataOnlyTitleWithView:(UIView*)view andOffsetY:(int)y andTitle:(NSString*)title andDelegate:(id<QLMaskNodataViewOnClickDelegate>) delegate
{
    [self hideNodataMaskWithView:view];
    NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"QLMaskNodataView" owner:self options:nil];
    QLMaskNodataView *nodataView = [nibViews objectAtIndex:0];
    nodataView.delegate = delegate;
    Class clazz = [UITableView class];
    Class parenClazz = [view class];
    if (clazz == parenClazz) {
        ((UITableView*)view).scrollEnabled = NO;
    }
    [view addSubview:nodataView];
    
    CGRect rect = CGRectMake(
                             nodataView.contentView.frame.origin.x,
                             (view.frame.size.height-nodataView.lableView.frame.size.height)/2+y,
                             nodataView.contentView.frame.size.width,
                             nodataView.lableView.frame.size.height);
    //隐藏imageview
    nodataView.imageView.hidden = YES;
    nodataView.lableView.frame = CGRectMake(nodataView.lableView.frame.origin.x, 0.0f, nodataView.lableView.frame.size.width, nodataView.lableView.frame.size.height);
    nodataView.contentView.frame = rect;
    if (title) {
        nodataView.lableView.text = title;
    }
}

+(void)showMaskNodataWithView:(UIView*)view andTitle:(NSString*)title
{
    [self showMaskNodataWithView:view andImage:nil andTitle:title];
}

+(void)showMaskNodataWithView:(UIView*)view andOffsetY:(int)y andTitle:(NSString*)title
{
    [self showMaskNodataWithView:view andOffsetY:y andImage:nil andTitle:title];
}

+(void)showMaskNodataWithView:(UIView*)view andOffsetY:(int)y andImage:(UIImage*)image andTitle:(NSString*)title
{
    [self showMaskNodataWithView:view andOffsetY:y andImage:image andTitle:title andDelegate:nil];
}

+(void)showMaskNodataWithView:(UIView*)view andImage:(UIImage*)image andTitle:(NSString*)title
{
    [self showMaskNodataWithView:view andOffsetY:0 andImage:image andTitle:title];
}

+(void)showMaskNodataWithView:(UIView*)view andImage:(UIImage*)image andTitle:(NSString*)title andDelegate:(id<QLMaskNodataViewOnClickDelegate>) delegate
{
    [self showMaskNodataWithView:view andOffsetY:0 andImage:image andTitle:title andDelegate:delegate];
}

+(void)hideNodataMaskWithView:(UIView*)view
{
    if (!view||!view.subviews) {
        return;
    }
    QLMaskNodataView *nodataView = nil;
    for (UIView *tmpView in view.subviews) {
        if ([tmpView class] == [QLMaskNodataView class]) {
            nodataView = (QLMaskNodataView*)tmpView;
            [nodataView removeFromSuperview];
            Class clazz = [UITableView class];
            Class parenClazz = [view class];
            if (clazz == parenClazz) {
                ((UITableView*)view).scrollEnabled = YES;
            }
        }
    }
}

@end
